using System;
using Gtk;
using System.Reflection;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using Notifications;

namespace uShareReal
{
	
	class MainClass
	{
		
		#region Statics fields
	
		public static string PersonnalPath = Environment.GetFolderPath(Environment.SpecialFolder.Personal)
			+ System.IO.Path.DirectorySeparatorChar.ToString()
			+ "." + Assembly.GetExecutingAssembly().GetName().Name 
			+ System.IO.Path.DirectorySeparatorChar.ToString();
		
		public static uShareReal.Objects.ConfigObject Config = null;
		public static Notification TrayNotification = null;
		public static StatusIcon TrayIcon = null;
		public static Menu TrayPopupMenu = null;
		
		#endregion
		
		/// <summary>
		/// Application entry point.
		/// </summary>
		/// <param name="args">
		/// A <see cref="System.String[]"/>
		/// </param>
		public static void Main (string[] args)
		{
			Mono.Unix.Catalog.Init("i18n", "./locale");
			
			if(!Directory.Exists(PersonnalPath))
				Directory.CreateDirectory(PersonnalPath);
			
			BinaryFormatter bf = new BinaryFormatter();			
			FileStream fs = new FileStream(PersonnalPath + "config.xml", FileMode.OpenOrCreate, FileAccess.Read, FileShare.None);
			try
			{
				MainClass.Config = bf.Deserialize(fs) as uShareReal.Objects.ConfigObject;
			}
			catch
			{
				MainClass.Config = new uShareReal.Objects.ConfigObject();
			}
			finally
			{
				if(fs != null)
					fs.Close();
					
				bf = null;
			}
			
			Application.Init ();
			MainWindow win = new MainWindow ();
			win.Show ();
			Application.Run ();
			
			bf = new BinaryFormatter();
			fs = new FileStream(PersonnalPath + "config.xml", FileMode.Create, FileAccess.Write, FileShare.None);
			try
			{
				bf.Serialize(fs, MainClass.Config);
			}
			catch { }
			finally
			{
				if(fs != null)
					fs.Close();
					
				bf = null;
			}
			
		}
		
	}
	
}

